﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using Microsoft.Practices.ServiceLocation;
using ModulesInfrastructure.ViewModels;
using ServerConnection;
using EventInfrastructure;
using ShellInfrastructure;
using ModuleProduct.Views;
using ServerConnection.ShopServiceReference;

namespace ModuleProduct.ViewModels
{
    public class ProductViewModel:SharedViewModel, IProductViewModel
    {

        #region private filds

        private CategoryView m_selectedCategory;
        private CategoryView m_selectedSubCategory;
        private ProductView m_selectedProduct;

        private IEventAggregator m_eventAggregator;

        private bool m_isInOrder=false;

        #endregion

        #region public proprties

        [Dependency]
        public IRegionManager RegionManager { get; set; }

        public List<CategoryView> LoadCategory { get; private set; }
        public List<CategoryView> LoadSubCategory { get; private set; }
        public List<ProductView> LoadProduct { get; private set; }

        public CategoryView OnSelectedCategory
        {
            get { return m_selectedCategory; }
            set
            {
                m_selectedCategory = value;
                if (m_selectedCategory != null)
                {
                    this.LoadSubCategory = Service.Instance.GetCategories("eng", m_selectedCategory.Id).ToList();
                    this.LoadProduct = null;
                    RaisePropertyChanged(() => this.LoadSubCategory);
                    RaisePropertyChanged(() => this.LoadProduct);
                    if(m_isInOrder.Equals(false))
                    m_eventAggregator.GetEvent<SelectCategoryEvent>().Publish(true);
                }
            }
        }

        public CategoryView OnSelectedSubCategory
        {
            get { return m_selectedSubCategory; }
            set
            {
                m_selectedSubCategory = value;
                if (m_selectedSubCategory != null)
                {
                    Dictionary<ProductFilter, object> filter = new Dictionary<ProductFilter, object>();
                    filter.Add(ProductFilter.Non, 0);
                    this.LoadProduct = Service.Instance.GetProductsOnPage(m_selectedSubCategory.Id, "eng", ProductSort.NameAsc, 0, 10, filter).ToList();
                    RaisePropertyChanged(() => this.LoadProduct);
                    if (m_isInOrder.Equals(false))
                    m_eventAggregator.GetEvent<SelectCategoryEvent>().Publish(true);
                }
            }
        }

        public ProductView OnSelectedProduct
        {
            get { return m_selectedProduct; }
            set
            {
                m_selectedProduct = value;
                if (m_selectedProduct != null)
                {
                    if (m_isInOrder.Equals(false))
                    {
                        m_eventAggregator.GetEvent<ProductSelectEvent>().Publish(m_selectedProduct.Id);
                        RaisePropertyChanged(() => this.LoadSubCategory);
                    }
                }
            }
        }

        #endregion

        #region constructor

        public ProductViewModel(IEventAggregator eventAggregator)
        {
            this.m_eventAggregator = eventAggregator;
            if (Service.Instance.GetCategories(Service.SelectedLanguage, 0)!=null)
                this.LoadCategory = Service.Instance.GetCategories(Service.SelectedLanguage, 0).ToList();
            m_eventAggregator.GetEvent<ShowProductEvent>().Subscribe(onShowProductEvent);
            m_eventAggregator.GetEvent<GetProductEditEvent>().Subscribe(onGetProductEditEvent);
            m_eventAggregator.GetEvent<ProductSaveEvent>().Subscribe(onProductSaveEvent);
            m_eventAggregator.GetEvent<CategorySaveEvent>().Subscribe(onCategorySaveEvent);
            m_eventAggregator.GetEvent<GetCategoryEditEvent>().Subscribe(onGetCategoryEditEvent);
            m_eventAggregator.GetEvent<GetAddSubCategoryEvent>().Subscribe(onGetAddSubCategoryEvent);
            m_eventAggregator.GetEvent<GetSubCategoryEditEvent>().Subscribe(onGetSubCategoryEditEvent);
            m_eventAggregator.GetEvent<OrderEditEvent>().Subscribe(onOrderEditEvent);
            m_eventAggregator.GetEvent<OrderAddEvent>().Subscribe(onOrderAddEvent);
            m_eventAggregator.GetEvent<GetAddProductInOrderEvent>().Subscribe(onGetAddProductInOrderEvent);
            m_isInOrder = false;
        }

        #endregion

        #region private functions

        private void onGetProductEditEvent(bool b)
        {
            if (m_selectedProduct != null)
                m_eventAggregator.GetEvent<ProductEditEvent>().Publish(m_selectedProduct.Id);
        }

        private void onProductSaveEvent(bool b)
        {
            if (m_selectedSubCategory != null)
            {
                Dictionary<ProductFilter, object> filter = new Dictionary<ProductFilter, object>();
                filter.Add(ProductFilter.Non, 0);
                this.LoadProduct = Service.Instance.GetProductsOnPage(m_selectedSubCategory.Id, Service.SelectedLanguage, ProductSort.NameAsc, 0, 10, filter).ToList();
                RaisePropertyChanged(() => this.LoadProduct);
            }
        }

        private void onCategorySaveEvent(bool b)
        {
            this.LoadCategory = Service.Instance.GetCategories(Service.SelectedLanguage, 0).ToList();
            RaisePropertyChanged(() => this.LoadCategory);
            RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            IRegion region = RegionManager.Regions[RegionNames.LeftRegionName];
            region.Activate(ServiceLocator.Current.GetInstance<IProductViewList>());
        }

        private void onGetCategoryEditEvent(bool b)
        {
            if (m_selectedCategory != null)
            {
                m_eventAggregator.GetEvent<CategoryEditEvent>().Publish(m_selectedCategory.Id);
            }
        }

        private void onGetAddSubCategoryEvent(bool b)
        {
            if (m_selectedCategory != null)
            {
                m_eventAggregator.GetEvent<AddSubCategoryEvent>().Publish(m_selectedCategory.Id);
            }
        }

        private void onGetSubCategoryEditEvent(bool b)
        {
            if (m_selectedCategory != null && m_selectedSubCategory != null)
            {
                m_eventAggregator.GetEvent<SubCategoryEditEvent>().Publish(m_selectedSubCategory.Id);
            }
        }

        private void onOrderAddEvent(bool b)
        {
            onOrderEditEvent(0);
        }

        private void onOrderEditEvent(long orderId)
        {
            m_isInOrder = true;
            if (Service.Instance.GetCategories(Service.SelectedLanguage, 0) != null && m_selectedCategory==null)
            {
                this.LoadCategory = Service.Instance.GetCategories(Service.SelectedLanguage, 0).ToList();
                RaisePropertyChanged(() => this.LoadCategory);
            }
            RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            IRegion region = RegionManager.Regions[RegionNames.LeftRegionName];
            region.Activate(ServiceLocator.Current.GetInstance<IProductViewList>());
        }

        private void onShowProductEvent(bool b)
        {
            m_isInOrder = false;
            if (Service.Instance.GetCategories(Service.SelectedLanguage, 0) != null && m_selectedCategory==null)
            {
                this.LoadCategory = Service.Instance.GetCategories(Service.SelectedLanguage, 0).ToList();
                RaisePropertyChanged(() => this.LoadCategory);
            }
            RegionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
            IRegion region = RegionManager.Regions[RegionNames.LeftRegionName];
            region.Activate(ServiceLocator.Current.GetInstance<IProductViewList>());
        }

        private void onGetAddProductInOrderEvent(bool b)
        {
            if (m_selectedCategory != null && m_selectedSubCategory != null && m_selectedProduct != null)
            {
                m_eventAggregator.GetEvent<AddProductInOrderEvent>().Publish(m_selectedProduct.Id);
            }
        }

        #endregion
        
    }
}
